using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace PageDraw
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            this.drawArea.FormMain = this;
            InitDrawTool();
        }
        // extend interface
        private void InitDrawTool()
        {
            // add ToolObject at here to extend
            tvDrawItem.Nodes["drawJButton"].Tag = new ToolJButton();
            tvDrawItem.Nodes["drawJLabel"].Tag = new ToolJLabel();
            tvDrawItem.Nodes["drawSelectableLabel"].Tag = new ToolSelectableLabel();
            tvDrawItem.Nodes["drawScrolledLabel"].Tag = new ToolScrolledLabel();
            tvDrawItem.Nodes["drawDateField"].Tag = new ToolDateField();
            tvDrawItem.Nodes["drawTimeField"].Tag = new ToolTimeField();
            tvDrawItem.Nodes["drawJCheckBox"].Tag = new ToolJCheckBox();
            tvDrawItem.Nodes["drawJRadioButton"].Tag = new ToolJRadioButton();
            tvDrawItem.Nodes["drawCodeSelectField"].Tag = new ToolCodeSelectField();
            tvDrawItem.Nodes["drawTorihikisakiCodeField"].Tag = new ToolTorihikisakiCodeField();
            tvDrawItem.Nodes["drawFormatedTextField"].Tag = new ToolFormatedTextField();
            tvDrawItem.Nodes["drawFormatedNumberField"].Tag = new ToolFormatedNumberField();
            tvDrawItem.Nodes["drawDataPanel"].Tag = new ToolDataPanel();
            tvDrawItem.Nodes["drawTableBasePanel"].Tag = new ToolTableBasePanel();
            tvDrawItem.Nodes["drawFixedTablePanel"].Tag = new ToolFixedTablePanel();
            tvDrawItem.Nodes["drawMySelectPanel"].Tag = new ToolMySelectPanel();
            tvDrawItem.Nodes["drawMainDataPanel"].Tag = new ToolMainDataPanel();
        }

        private void tvDrawItem_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.drawArea.DrawTool = tvDrawItem.SelectedNode.Tag as ToolObject;
        }
       
        public void ResetDrawTool()
        {
            tvDrawItem.SelectedNode = tvDrawItem.Nodes["point"];
        }

        private void toolStrip_Click(object sender, EventArgs e)
        {
            ToolStripButton tb = sender as ToolStripButton;
            if (tb == tbLeftView)
            {
                LeftViewControl(tb);
            }
            else if (tb == tbRightView)
            {
                RightViewControl(tb);
            }
            else if (tb == tbUpAlign)
            {
                drawArea.AlignHandle(Align.UP);
            }
            else if (tb == tbDownAlign)
            {
                drawArea.AlignHandle(Align.Down);
            }
            else if (tb == tbLeftAlign)
            {
                drawArea.AlignHandle(Align.Left);
            }
            else if (tb == tbRightAlign)
            {
                drawArea.AlignHandle(Align.Right);
            }
            else if (tb == tbDelete)
            {
                drawArea.GraphicsTreeMgr.DeleteSelectedDrawObjects();
            }
            else if (tb == tbSelectAll)
            {
                drawArea.GraphicsTreeMgr.SelectAll();
            }
            else if (tb == tbUpMove)
            {
                drawArea.MoveObjects(Align.UP);
            }
            else if (tb == tbDownMove)
            {
                drawArea.MoveObjects(Align.Down);
            }
            else if (tb == tbLeftMove)
            {
                drawArea.MoveObjects(Align.Left);
            }
            else if (tb == tbRightMove)
            {
                drawArea.MoveObjects(Align.Right);
            }
            else if (tb == tbCut)
            {
                drawArea.GraphicsTreeMgr.CutObjects();
            }
            else if (tb == tbCopy)
            {
                drawArea.GraphicsTreeMgr.CopyObjects();
            }
            else if (tb == tbPaste)
            {
                drawArea.GraphicsTreeMgr.PasteObjects();
            }
            else if (tb == tbFrontMove)
            {
                drawArea.GraphicsTreeMgr.DirectionMove(Direction.Front);
            }
            else if (tb == tbBackMove)
            {
                drawArea.GraphicsTreeMgr.DirectionMove(Direction.Back);
            }
            else if (tb == tbTopMost)
            {
                TopMostFormMain();
            }
            else if (tb == tbFormLock)
            {
                LockForm();
            }
            else if (tb == tbCoordinate)
            {
                CoordinateControl();
            }
            else if (tb == tbCross)
            {
                CrossControl();
            }
            else if (tb == tbSave)
            {
                SaveAsObj();
            }
            else if (tb == tbSaveAsXml)
            {
                SaveAsXml();
            }
            else if (tb == tbOpen)
            {
                OpenObj();
            }
        }

        private void CrossControl()
        {
            if (!drawArea.IsDrawCross)
            {
                tbCross.Image = Properties.Resources.Cross;
                drawArea.IsDrawCross = true;
            }
            else
            {
                tbCross.Image = Properties.Resources.NoCross;
                drawArea.IsDrawCross = false;
            }
            drawArea.Refresh();
        }

        private void CoordinateControl()
        {
            if (!drawArea.IsDrawCoordinate)
            {
                tbCoordinate.Image = Properties.Resources.Coordinate;
                drawArea.IsDrawCoordinate = true;
            }
            else
            {
                tbCoordinate.Image = Properties.Resources.blank;
                drawArea.IsDrawCoordinate = false;
            }
            drawArea.Refresh();
        }

        private void LockForm()
        {
            if (this.FormBorderStyle == FormBorderStyle.Sizable)
            {
                this.FormBorderStyle = FormBorderStyle.FixedSingle;
                this.tbFormLock.Image = Properties.Resources.Lock;
                this.MaximizeBox = false;
            }
            else
            {
                this.FormBorderStyle = FormBorderStyle.Sizable;
                this.tbFormLock.Image = Properties.Resources.ReSize;
                this.MaximizeBox = true;
            }
        }

        private void TopMostFormMain()
        {
            this.TopMost = !this.TopMost;
            if (this.TopMost)
            {
                this.tbTopMost.Image = Properties.Resources.topMost;
            }
            else
            {
                this.tbTopMost.Image = Properties.Resources.unTopMost;
            }
        }

        private void RightViewControl(ToolStripButton tb)
        {
            if (splitContainerR.Panel2Collapsed)
            {
                tb.Image = Properties.Resources.GoLtrHS;
            }
            else
            {
                tb.Image = Properties.Resources.GoRtlHS;
            }
            splitContainerR.Panel2Collapsed = !splitContainerR.Panel2Collapsed;
        }

        private void LeftViewControl(ToolStripButton tb)
        {
            if (splitContainerL.Panel1Collapsed)
            {
                tb.Image = Properties.Resources.GoRtlHS;
            }
            else
            {
                tb.Image = Properties.Resources.GoLtrHS;
            }
            splitContainerL.Panel1Collapsed = !splitContainerL.Panel1Collapsed;
        }

        private void OpenObj()
        {
            openFileDialog.Filter = "(PageDraw File)*.dat|*.dat";
            openFileDialog.FileName = string.Empty;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (!drawArea.GraphicsTreeMgr.OpenFronObj(openFileDialog.FileName))
                {
                    MessageBox.Show("Open failed!");
                }
            }
        }

        private void SaveAsXml()
        {
            if (drawArea.GraphicsTree.DrawObjects.Count != 0)
            {
                if (!string.IsNullOrEmpty(saveFileDialog1.FileName))
                {
                    if (!drawArea.GraphicsTreeMgr.SaveToXML(saveFileDialog1.FileName))
                    {
                        MessageBox.Show("Save failed!");
                    }
                    else
                    {
                        setTitle("PageDraw");
                    }
                }
                else
                {
                    saveFileDialog1.Filter = "(XML File)*.xml|*.xml";
                    saveFileDialog1.FileName = "pageDraw_" + DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss");
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        if (!drawArea.GraphicsTreeMgr.SaveToXML(saveFileDialog1.FileName))
                        {
                            MessageBox.Show("Save failed!");
                        }
                        else
                        {
                            setTitle("PageDraw");
                        }
                    }
                }
            }
        }

        private void SaveAsObj()
        {
            if (drawArea.GraphicsTree.DrawObjects.Count != 0)
            {
                if (!string.IsNullOrEmpty(saveFileDialog.FileName))
                {
                    if (!drawArea.GraphicsTreeMgr.SaveToObj(saveFileDialog.FileName))
                    {
                        MessageBox.Show("Save failed!");
                    }
                    else
                    {
                        setTitle("PageDraw");
                    }
                }
                else
                {
                    saveFileDialog.Filter = "(PageDraw File)*.dat|*.dat";
                    saveFileDialog.FileName = "pageDraw_" + DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss");
                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        if (!drawArea.GraphicsTreeMgr.SaveToObj(saveFileDialog.FileName))
                        {
                            MessageBox.Show("Save failed!");
                        }
                        else
                        {
                            setTitle("PageDraw");
                        }
                    }
                }
            }
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            if (item == selectAllToolStripMenuItem)
            {
                drawArea.GraphicsTreeMgr.SelectAll();
            }
            else if (item == deleteToolStripMenuItem)
            {
                drawArea.GraphicsTreeMgr.DeleteSelectedDrawObjects();
            }
            else if (item == copyToolStripMenuItem)
            {
                drawArea.GraphicsTreeMgr.CopyObjects();
            }
            else if (item == pasteToolStripMenuItem)
            {
                drawArea.GraphicsTreeMgr.PasteObjects();
            }
            else if (item == cutToolStripMenuItem)
            {
                drawArea.GraphicsTreeMgr.CutObjects();
            }
            else if (item == frontMoveToolStripMenuItem)
            {
                drawArea.GraphicsTreeMgr.DirectionMove(Direction.Front);
            }
            else if (item == backMoveToolStripMenuItem)
            {
                drawArea.GraphicsTreeMgr.DirectionMove(Direction.Back);
            }
            else if (item == upAlignToolStripMenuItem)
            {
                drawArea.GraphicsTreeMgr.AlignHandle(Align.UP);
            }
            else if (item == downAlignToolStripMenuItem)
            {
                drawArea.GraphicsTreeMgr.AlignHandle(Align.Down);
            }
            else if (item == leftAlignToolStripMenuItem)
            {
                drawArea.GraphicsTreeMgr.AlignHandle(Align.Left);
            }
            else if (item == rightAlignToolStripMenuItem)
            {
                drawArea.GraphicsTreeMgr.AlignHandle(Align.Right);
            }
            else if (item == saveAsdatToolStripMenuItem)
            {
                tbSave.PerformClick();
            }
            else if (item == saveAsxmlToolStripMenuItem)
            {
                tbSaveAsXml.PerformClick();
            }
            drawArea.Refresh();
        }

        public void setMouseLocation(Point point)
        {
            this.tsslL.Text = "( " + point.X + ", " + point.Y + " )";
        }

        public void setTitle(string title)
        {
            this.Text = title;
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.Text.Equals("PageDraw *"))
            {
                DialogResult res = MessageBox.Show("The result hasn't saved, are you sure to exit.", "warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (res == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }
        }

        public void SetPropGridSelectedObject(Object drawObject)
        {
            if (drawObject is NoPropertyObject)
            {
                if (!splitContainerR.Panel2Collapsed)
                {
                    tbRightView.PerformClick();
                }
            }
            else
            {
                if (splitContainerR.Panel2Collapsed)
                {
                    tbRightView.PerformClick();
                }
            }
            this.propertyGrid.SelectedObject = drawObject;
        }

        private void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            this.drawArea.Refresh();
            propertyGrid.Refresh();
        }

        public void ResizeDrawArea()
        {
            bool flg = splitContainerL.Panel1Collapsed && splitContainerR.Panel2Collapsed;// bool && bool
            if (!flg) // false && false, true && false, false && true == false
            {
                if (!splitContainerL.Panel1Collapsed)
                {
                    tbLeftView.PerformClick();
                }
                if (!splitContainerR.Panel2Collapsed)
                {
                    tbRightView.PerformClick();
                }
            }
            else // true && true == true
            {
                tbLeftView.PerformClick();
                tbRightView.PerformClick();
            }
        }

        private void FormMain_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("FileNameW"))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void FormMain_DragDrop(object sender, DragEventArgs e)
        {
            string[] fileNames = null;
            if (e.Data.GetDataPresent("FileNameW"))
            {
                fileNames = (string[])e.Data.GetData("FileNameW");
                if (fileNames != null && fileNames.Length > 0)
                {
                    if (Path.HasExtension(fileNames[0]))
                    {
                        if (Path.GetExtension(fileNames[0]) == ".dat")
                        {
                            if (!drawArea.GraphicsTreeMgr.OpenFronObj(fileNames[0]))
                            {
                                MessageBox.Show("Open failed!");
                            }
                            else
                            {
                                saveFileDialog.FileName = fileNames[0];
                            }
                        }
                    }
                }
            }
        }
    }

    public enum Align
    { 
        UP,
        Down,
        Left,
        Right
    }

    public enum Direction
    { 
        Front,
        Back
    }
}